﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>用户区域地址</title>
    <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/demo.css">
    <script type="text/javascript" src="~/Content/jquery-easyui-1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="~/Content/jquery-easyui-1.4.2/jquery.easyui.min.js"></script>
    <script src="~/Content/jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"></script>
    <link href="~/Content/Script/jquery.Jcrop.css" rel="stylesheet" />
    <script src="~/Content/jquery-easyui-1.4.2/jquery.Jcrop.js"></script>

    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入 Vue -->
    <script src="https://unpkg.com/vue"></script>
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>

    <style>
        .dig-table {
            border: 0px solid black;
            line-height: 32px;
            width: 90%;
        }

        .box {
            position: fixed;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.2);
            display: none;
        }

        td label {
            padding-left: 5px;
            color: red;
        }

        #app {
            padding: 15px;
            background-color: #e6eef8b3;
        }

        .buttons {
            margin-bottom: 15px;
        }
    </style>
</head>
<body style="padding: 0">
    <div id="app">
        <div class="buttons">
            <el-button type="info" v-on:click="add" style="background-color: #50bfff;border-color: #50bfff;">添加下级</el-button>
        </div>
        <el-row :gutter="20">
            <el-col :span="6">
                <el-tree :data="data2" ref="tree" show-checkbox node-key="DistrictID" check-strictly highlight-current :props="defaultProps" default-expand-all v-on:node-click="handleNodeClick"></el-tree>
            </el-col>
            <el-col :span="18">
                <el-table :data="tableData"
                          border
                          style="width: 100%">
                    <el-table-column fixed
                                     prop="DistrictName"
                                     label="区域名称"
                                     width="300">
                    </el-table-column>
                    <el-table-column prop="DelFormMon"
                                     label="配送费"
                                     width="120">
                    </el-table-column>
                    <el-table-column prop="DistrictState"
                                     label="启用状态"
                                     width="100">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.DistrictState == 1 ? 'primary' : 'warning'"
                                    disable-transitions>{{ChangeDistrictState(scope.row.DistrictState)}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作"
                                     width="150">
                        <template slot-scope="scope">
                            <el-button size="mini"
                                       v-on:click="revise(scope.$index, scope.row)">修改</el-button>
                            <el-button size="mini"
                                       type="danger"
                                       :disabled="DelDisabled(scope.$index, scope.row)"
                                       v-on:click="del(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>

        @*弹窗对话框*@
        <el-dialog title="区域地址" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="区域ID" :label-width="formLabelWidth" v-if="0">
                    <el-input v-model="form.DistrictID" auto-complete="off"></el-input>
                    <el-input v-model="form.TagType" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="地址名称" :label-width="formLabelWidth">
                    <el-input v-model="form.DistrictName" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="配送费" :label-width="formLabelWidth">
                    <el-input v-model="form.DelFormMon" auto-complete="off" v-on:change="NumberCheck()"></el-input>
                </el-form-item>
                <el-form-item label="启用状态" :label-width="formLabelWidth">
                    <el-select v-model="form.DistrictState" placeholder="请选择启用状态">
                        <el-option label="启用" value="1"></el-option>
                        <el-option label="不启用" value="0"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button v-on:click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" v-on:click="dialogFormSave">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</body>
</html>

<script>
    var uddata = "";
    $.ajax({
        url: "/User_District/getgrids",
        type: "Post",
        dataType: 'json',
        async: false,
        data: {},
        success: function (data) {
            //console.log(data);
            if (data.BS == '1') {
                uddata = data.rows[0];
            }
        }
    });

    var Main = {
        data: function data() {
            return {
                data2: [uddata],
                defaultProps: {
                    children:
                        'Children',
                    label:
                        'DistrictName'
                },
                tableData: [],
                dialogFormVisible: false,
                form: {
                    DistrictID: '',
                    DistrictName: '',
                    DelFormMon: '',
                    DistrictState: '',
                    TagType: 0,
                },
                formLabelWidth: '120px',
            };
        },
        methods: {
            handleNodeClick(data) {
                var _this = this;
                //console.log(data);
                $.ajax({
                    url: "/User_District/User_Districts_Details",
                    type: "Post",
                    dataType: 'json',
                    async: false,
                    data: { DistrictID: data.DistrictID },
                    success: function (result) {
                        //console.log(result);
                        _this.tableData = result.rows;
                    }
                });
            },
            add: function add() {
                //console.log(this.$refs.tree.getCheckedNodes());
                //console.log(this.$refs.tree.getCheckedKeys());
                if (this.$refs.tree.getCheckedKeys().length != 1) {
                    this.$message({
                        type: 'error',
                        message: '请确保只勾选一个节点！'
                    });
                }
                else if (this.$refs.tree.getCheckedNodes()[0].DistrictID.length >= 9) {
                    this.$message({
                        type: 'warning',
                        message: '该第三节点不能再添加数据咯！'
                    });
                }
                else {
                    var _this = this;
                    this.clearText();
                    _this.form.TagType = 1;
                    _this.dialogFormVisible = true;
                }
            },
            revise: function revise(index, row) {
                //console.log(row.DistrictID);
                //console.log(row.DistrictName);
                //console.log(row.DelFormMon);
                //console.log(row.DistrictState);
                var _this = this;
                this.clearText();
                _this.form.DistrictID = row.DistrictID;
                _this.form.DistrictName = row.DistrictName;
                _this.form.DelFormMon = row.DelFormMon;
                _this.form.DistrictState = row.DistrictState == '1' ? "启用" : row.DistrictState == '0' ? "不启用" : "";
                _this.form.TagType = 2;
                _this.dialogFormVisible = true;
            },
            clearText() {
                var _this = this;
                _this.form.DistrictID = '';
                _this.form.DistrictName = '';
                _this.form.DelFormMon = '';
                _this.form.DistrictState = '';
                _this.form.TagType = 0;
            },
            dialogFormSave: function dialogFormSave() {
                var _this = this;
                var Tag = 0;
                var sut = "";
                if (_this.form.TagType == 1) {
                    Tag = _this.form.TagType;
                    sut = _this.$refs.tree.getCheckedKeys();
                }
                else {
                    Tag = 2;
                    if (_this.form.DistrictState == '启用' || _this.form.DistrictState == '不启用') {
                        _this.form.DistrictState = _this.form.DistrictState == '启用' ? "1" : _this.form.DistrictState == '不启用' ? "0" : "";
                    }
                    sut = _this.form.DistrictID;
                }
                if (sut == "") {
                    this.$message({
                        showClose: true,
                        message: '区域ID不能为空',
                        type: 'error'
                    });
                }
                else if (_this.form.DistrictName == "") {
                    this.$message({
                        showClose: true,
                        message: '地址名称不能为空',
                        type: 'error'
                    });
                }
                else if (_this.form.DelFormMon == "") {
                    this.$message({
                        showClose: true,
                        message: '配送费不能为空',
                        type: 'error'
                    });
                }
                else if (_this.form.DistrictState == "") {
                    this.$message({
                        showClose: true,
                        message: '请选择启用状态',
                        type: 'error'
                    });
                }
                else {
                    //console.log(_this.$refs.tree.getCheckedKeys());
                    //console.log(_this.form.DistrictName);
                    //console.log(_this.form.DelFormMon);
                    //console.log(_this.form.DistrictState);
                    //console.log(Tag);
                    $.ajax({
                        url: "/User_District/UpdateUser_District",
                        type: "Post",
                        dataType: 'json',
                        data: {
                            "DistrictID": _this.form.TagType == '1' ? _this.$refs.tree.getCheckedKeys()[0] : _this.form.TagType == '2' ? _this.form.DistrictID : "",
                            "DistrictName": _this.form.DistrictName,
                            "DelFormMon": _this.form.DelFormMon,
                            "DistrictState": _this.form.DistrictState,
                            "Tag": Tag
                        },
                        success: function (result) {
                            //console.log(result);
                            if (result == '1') {
                                $.ajax({
                                    url: "/User_District/getgrids",
                                    type: "Post",
                                    dataType: 'json',
                                    data: {},
                                    success: function (resp) {
                                        if (resp.BS == '1') {
                                            _this.data2 = [resp.rows[0]];
                                        }
                                    }
                                });
                                _this.$message({
                                    type: 'success',
                                    message: "更新成功"
                                });
                                _this.tableData = [];
                                _this.dialogFormVisible = false;
                            }
                            else {
                                _this.$message({
                                    type: 'error',
                                    message: result
                                });
                            }
                        }
                    });
                }
            },
            del: function del(index, row) {
                this.$confirm('你确定要删除  ' + row.DistrictName + '  该区域地址吗？',
                    '删除',
                    {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消'
                    })
                    .then(() => {
                        var $this = this;
                        $.ajax({
                            url: "/User_District/UpdateUser_District",
                            type: "Post",
                            dataType: 'json',
                            data: {
                                "DistrictID": row.DistrictID,
                                "DistrictName": row.DistrictName,
                                "DelFormMon": row.DelFormMon,
                                "DistrictState": row.DistrictState,
                                "Tag": 3
                            },
                            success: function (result) {
                                //console.log(result);
                                if (result == '1') {
                                    $.ajax({
                                        url: "/User_District/getgrids",
                                        type: "Post",
                                        dataType: 'json',
                                        data: {},
                                        success: function (resp) {
                                            if (resp.BS == '1') {
                                                $this.data2 = [resp.rows[0]];
                                            }
                                        }
                                    });
                                    $this.$message({
                                        type: 'success',
                                        message: "删除成功"
                                    });
                                    $this.tableData = [];
                                    $this.dialogFormVisible = false;
                                }
                                else {
                                    $this.$message({
                                        type: 'error',
                                        message: result
                                    });
                                }
                            }
                        });
                    })
                    .catch
                    (() => {
                        this.$message({
                            type: 'warning',
                            message: '你取消了操作'
                        });
                    });
            },
            NumberCheck: function NumberCheck() {
                let reg = /[^\d\.]/g;
                if (this.form.DelFormMon && new RegExp(reg).test(this.form.DelFormMon)) {   //非数字
                    this.$message({
                        message: '请输入数字或小数',
                        type: 'warning'
                    })
                    this.form.DelFormMon = '';
                    return;
                }
            },
            DelDisabled(index, row) {
                //console.log(index);
                //console.log(row);
                var result = '';
                if (row.DistrictID.length == 3) {
                    result = true;
                }
                else {
                    result = false;
                }
                return result;
            },
            ChangeDistrictState(row) {
                //console.log(row);
                var DistrictStateText = '';
                if (row == 0) {
                    DistrictStateText = "不启用";
                }
                else if (row == 1) {
                    DistrictStateText = "启用";
                }
                return DistrictStateText;
            },


        }
    }

    var Ctor = Vue.extend(Main);
    new Ctor().$mount('#app');

</script>